package com.ecarx.module_vehicle.signal.adatpter;

import android.annotation.SuppressLint;
import android.content.res.ColorStateList;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.ecarx.library_base.BaseApplication;
import com.ecarx.module_vehicle.R;
import com.ecarx.module_vehicle.signal.Model.SignalModel;

import java.util.ArrayList;
import java.util.List;

public class BusinessAdapter extends RecyclerView.Adapter<BusinessAdapter.MyViewHolder> {
    private List<String> mDataList = new ArrayList<>();
    private final onItemClickListener onItemClickListener;
    private final ColorStateList colorDef;
    private final ColorStateList colorSelect;
    private ViewGroup parent;
    private String selectedModule;

    public BusinessAdapter(BusinessAdapter.onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
        colorDef = ColorStateList.valueOf(BaseApplication.getInstance().getColor(R.color.color_theme_text));
        colorSelect = ColorStateList.valueOf(BaseApplication.getInstance().getColor(R.color.color_theme_blue));
    }

    public void setList(List<String> datas) {
        this.mDataList = datas;
        selectedModule = datas.get(0);
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        this.parent = parent;
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_top_business, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        view.setTag(myViewHolder);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, @SuppressLint("RecyclerView") int position) {
        String bean = mDataList.get(position);
        holder.tvName.setText(bean);
        boolean isSelected = selectedModule.equals(bean);
        holder.tvName.setTextColor(isSelected ? colorSelect : colorDef);
        holder.imageView.setImageResource(isSelected ? SignalModel.getModuleSelectedDrawable(bean) : SignalModel.getModuleDrawable(bean));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selectedModule = bean;
                refreshSelectedView(bean);
                onItemClickListener.onItemClick(view, position, bean);
            }
        });
    }

    public void refreshSelectedView(String selectedName) {

        int groupCount = parent.getChildCount();

        for (int i = 0; i < groupCount; i++) {
            View view = parent.getChildAt(i);
            MyViewHolder myViewHolder = (MyViewHolder) view.getTag();
            String module = myViewHolder.tvName.getText().toString();
            boolean isSelected = module.equals(selectedName);

            //从viewTag中取出文字名称进行匹配，对应上的就要变蓝色

            myViewHolder.tvName.setTextColor(isSelected ? colorSelect : colorDef);
            myViewHolder.imageView.setImageResource(isSelected ? SignalModel.getModuleSelectedDrawable(module) : SignalModel.getModuleDrawable(module));
        }
    }


    @Override
    public int getItemCount() {
        return mDataList.size();
    }


    static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tvName;
        ImageView imageView;

        public MyViewHolder(View itemView) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.tv);
            imageView = (ImageView) itemView.findViewById(R.id.iv);
        }

    }

    public interface onItemClickListener {
        void onItemClick(View view, int postion, Object o);
    }
}
